<?php

namespace app\data\controller\control;
use app\data\model\EnterStorehouseItemModel;
use app\data\model\StorehouseModel;
use think\admin\Controller;
use think\facade\Db;


/**
 * 入库明细管理
 * Class Goods
 * @package app\data\controller\shop
 */
class EnterDetailed extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'SnEnterStorehouseItem';

    /**
     * 最大分类等级
     * @var integer
     */
    protected $cateLevel;

    /**
     * 入库明细管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '入库明细管理';
        $query = $this->_query($this->table);
        if(input('material_code')){
            $where[] = ['material_code','like',input('material_code').'%'];
            $query->where($where);
        }
        // 列表排序并显示
        $query->like('material_name,material_model');
        $query->equal('status,storehouse_id')->order('material_code asc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {
        foreach ($data as $k=>$v){
            $data[$k]['storehouse_name'] = StorehouseModel::where('id',$v['storehouse_id'])->where('deleted',0)->value('name');
        }
        $this->storehouse_list = StorehouseModel::where('deleted',0)->select()->toArray();
    }
    
    /**
     * 物资数量清空
     * @auth true
     */
    public function give_back_num()
    {
        $input = input('post.');
        $ControlClaimList = new EnterStorehouseItemModel();
        $info = $ControlClaimList->where('id',$input['id'])->find()->toArray();
        if(!$info){$this->error('数据不存在');}
        //开启事务
        Db::startTrans();
        try{
            //库存数据自减
            if(! Db::name('sn_storehouse_data')->where('material_code',$info['material_code'])->dec('material_num',$info['material_num'])->update()) throw new \Exception("服务器错误");
            $material_xjprice = sprintf("%01.2f", $info['material_price'] * $info['material_num']);
            if(! Db::name('sn_storehouse_data')->where('material_code',$info['material_code'])->dec('material_xjprice',$material_xjprice)->update()) throw new \Exception("服务器错误");
            $saveArr = array(
                'material_num'=>0,
                'material_xjprice'=>0,
            );
            $saveinfo = EnterStorehouseItemModel::where('id',$input['id'])->update($saveArr);
            if($saveinfo === false){return json(['code'=>500,'msg'=>'服务器繁忙']);}
            // 执行提交操作
            Db::commit();
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 获取提示信息
            $this->error($e->getMessage());die;
        }
        $this->success('数据更新成功');
    }


}